from playwright import sync_api
import time
import json
import os
import sys
import re
import random
import json
import datetime
import hashlib
import base64
import urllib.parse
import urllib.request
import threading

page_num = 1
max_page_num = 193

path = os.path.dirname(os.path.abspath(__file__))


def login():
    playwright = sync_api.sync_playwright().start()
    browser = playwright.chromium.launch(headless=False, slow_mo=50)
    context = browser.new_context()
    page = context.new_page()

    if not os.path.exists(os.path.join(path, "datas")):
        os.makedirs(os.path.join(path, "datas"))
    page.goto('https://gitee.com/login')
    name = ""  # 输入用户名
    name_input = page.query_selector("#user_login")
    name_input.fill(name)
    password = ""  # 输入密码
    password_input = page.query_selector("#user_password")
    password_input.fill(password)
    user_remember_me = page.query_selector(
        "#new_user > div > div > div > div.two.fields > div:nth-child(1) > div")
    user_remember_me.click()
    page.query_selector(
        "#new_user > div > div > div > div:nth-child(4) > input").click()
    page.wait_for_timeout(3000*random.random())
    return page, browser


def get_projects():
    global page_num
    projects = []
    page, browser = login()
    page.goto("https://gitee.com/organizations/openkylin/issues")
    max_page_num = page.query_selector(
        "#git-discover-page > a:nth-child(6)").inner_text()
    while page_num <= int(max_page_num):
        page.goto(
            f'https://gitee.com/organizations/openkylin/projects?page={page_num}')
        name_input = page.query_selector("#user_login")
        if name_input:
            page, browser = login()
            page.goto(
                f'https://gitee.com/organizations/openkylin/projects?page={page_num}')
        if not page.query_selector("#user_login"):
            projectdiv = page.query_selector(
                "#git-group-project > div > div.column.twelve.wide > div.project-container")
            if projectdiv:
                projectlist = projectdiv.query_selector_all(
                    "div.project-list-item")
                for project in projectlist:
                    projectname = project.query_selector(
                        ".repository").inner_text()
                    projecturl = project.query_selector(
                        ".repository").get_attribute("href")
                    description = project.query_selector(
                        ".description").inner_text()
                    projecturl = "https://gitee.com/"+projecturl
                    projects.append((projectname, projecturl, description))

            with open(os.path.join(path, "datas", "gitee_project.json"), "w") as f:
                json.dump(projects, f, ensure_ascii=False, indent=4)
            page.wait_for_timeout(3000*random.random())
        page_num += 1

    browser.close()


def get_tasks():
    page, browser = login()
    page_num = 1
    page.wait_for_timeout(3000*random.random())
    tasks = {}
    path = os.path.dirname(__file__)
    data_path = os.path.join(path, "datas")
    if not os.path.exists(data_path):
        os.makedirs(data_path)
    page.goto(
        "https://gitee.com/organizations/openkylin/issues?search_source=filter_box&state=all")
    max_page_num = page.query_selector(
        "#git-discover-page > a:nth-child(6)").inner_text()
    print(max_page_num)
    max_page_num = 10
    while page_num <= int(max_page_num):
        page.goto(
            f"https://gitee.com/organizations/openkylin/issues?page={page_num}&search_source=filter_box&state=all")
        name_input = page.query_selector("#user_login")
        if name_input:
            browser.close()
            page, browser = login()
            page.goto(
                f'https://gitee.com/organizations/openkylin/issues?page={page_num}&search_source=filter_box&state=all')
        if not page.query_selector("#user_login"):
            for i in range(random.randint(10, 30)):
                page.mouse.wheel(0, random.randint(100, 200))
            all_tasks = page.query_selector(
                "#git-group-content > div.ui.flat.nopadding.git-issues-panel > div.panel-list.mt-2")
            if all_tasks:
                all_tasks = all_tasks.query_selector_all(".issue-list")
                urls = []
                for task in all_tasks:
                    url = "https://gitee.com"+task.query_selector(
                        ".title").get_attribute("href")
                    urls.append(url)
                for url in urls:
                    page.goto(url)
                    for i in range(random.randint(5, 10)):
                        page.mouse.wheel(0, random.randint(100, 200))
                    project_name = url.split("/")[-3]
                    if project_name not in tasks:
                        tasks[project_name] = []
                    tags = page.query_selector("#js-labels-list")
                    tags = [tag.inner_text()
                            for tag in tags.query_selector_all(".ui")]
                    title = page.query_selector(
                        "#git-issue-title").inner_text()
                    status = page.query_selector(
                        "#issue-state-button > div.left.text.d-align-center.d-flex > div").inner_text()
                    issue_id = url.split("/")[-1]
                    author = page.query_selector(".author").inner_text()
                    selected_users = page.query_selector(
                        "#issue-collaborators > div.selected-users > img")
                    if selected_users:
                        selected_users = [user.get_attribute(
                            "alt") for user in selected_users.query_selector_all("img")]
                    else:
                        selected_users = []
                    combo = page.query_selector(
                        "div.issue-field.reactions-pr-field > .content").inner_text()
                    data = {
                        "id": issue_id,
                        "title": title,
                        "url": url,
                        "tags": tags,
                        "status": status,
                        "author": author,
                        "combo": combo,
                        "selected_users": selected_users,
                    }
                    print(data)
                    tasks[project_name].append(data)
            with open(os.path.join(data_path, "gitee_tasks.json"), "w") as f:
                json.dump(tasks, f, ensure_ascii=False, indent=4)
            page.wait_for_timeout(3000*random.random())
        page_num += 1

    browser.close()
    control_path = os.path.join(os.path.dirname(path), "spider_control.json")
    with open(control_path, "r") as f:
        spiders = json.load(f)
        for spider in spiders:
            if spider["name"] == "gitee":
                spider["status"] = "finish"
                break
        with open(control_path, "w") as f:
            json.dump(spiders, f, ensure_ascii=False, indent=4)
            print("gitee finish")


if __name__ == '__main__':
    # get_projects()
    get_tasks()
